package cn.tedu.emqx.demo.emqx;

import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class MySerializer<T> {
    public byte[] serialize(T t) {
        // 实现序列化逻辑 把object对象 转化成 byte[]
        return JSON.toJSONString(t).getBytes(StandardCharsets.UTF_8);
    }
    public T deserialize(byte[] bytes, Class<T> clazz) {
        // 实现反序列化逻辑 把byte[] 转化成 object对象
        return JSON.parseObject(new String(bytes, StandardCharsets.UTF_8), clazz);
    }
}
